package com.mhsoft.fenak;

import java.util.ArrayList;
import java.util.HashMap;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import com.mhsoft.fenak.utils.ConnectionManager;
import com.mhsoft.fenak.utils.Constants;
import com.mhsoft.fenak.utils.JSONParser;
import com.mhsoft.fenak.utils.TextValidation;
import com.mhsoft.fenak.utils.UpdateUI_Interface;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class SignInActivity extends Activity implements OnClickListener, UpdateUI_Interface {
	
	private EditText emailEditText, passEditText;
	private Button forgetPassButton, registerButton, signInButton;
    /** Called when the activity is first created. */
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.sign_in);
        
        emailEditText=(EditText)findViewById(R.id.sign_email_editText);
        passEditText=(EditText)findViewById(R.id.sign_pass_editText);
        
        forgetPassButton=(Button)findViewById(R.id.sign_forgetpass_button);
        registerButton=(Button)findViewById(R.id.sign_register_button);
        signInButton=(Button)findViewById(R.id.sign_signin_button);
        
        forgetPassButton.setOnClickListener(this);
        registerButton.setOnClickListener(this);
        signInButton.setOnClickListener(this);
    }
	@Override
	public void onClick(View v) {
		if(v==forgetPassButton){
			new forgetPassDialog(this).show();
		}else if(v==registerButton){
			startActivityForResult(new Intent(this, RegistrationActivity.class),11);
		}else if(v==signInButton){
			if (TextValidation.validateSignIn(this, emailEditText.getText().toString() , passEditText.getText().toString())) {
				ArrayList<NameValuePair> dataString=new ArrayList<NameValuePair>();
				dataString.add(new BasicNameValuePair("user_email", emailEditText.getText().toString()));
				dataString.add(new BasicNameValuePair("password", passEditText.getText().toString()));
//				ConnectionManager.doPostRequest(this, "Signing in", "Please wait...", Constants.URL_SIGN_IN, "1", dataString);
				ConnectionManager.doGetRequest(this, "Signing in", "Please wait..."
						, Constants.URL_SIGN_IN+"?userName="+emailEditText.getText().toString()+"&password="+passEditText.getText().toString(), "1");
			}else{
				
			}
		}
	}
	@Override
	public void updateUI(String[] responseString) {
		
		if(responseString[0].equals("200") && Integer.parseInt(responseString[2]) == 1) {
			
			HashMap<String, Object> response =  (HashMap<String, Object>) JSONParser.convertJSON2Hashmap(responseString[1]);
			
			if(response.get("post") != null){
				
				if(response.get("post").equals("0")){
					
					// no such user ...
					Toast.makeText(this, getString(R.string.notFoundUser), Toast.LENGTH_SHORT).show();
				}
				else if(response.get("post").equals("1")){
					
					// incorrect password ...
					Toast.makeText(this, getString(R.string.incorrectPass), Toast.LENGTH_SHORT).show();
				}
			}
			else{
				
				Toast.makeText(this, getString(R.string.successfulLogin), Toast.LENGTH_SHORT).show();
				
				Log.d("USER_ID", (String)response.get("usrid"));
				Log.d("USER_FNAME", (String)response.get("usr_fname"));
				Log.d("USER_LNAME", (String)response.get("usr_lname"));
				Log.d("EMAIL", (String)response.get("email"));
				Log.d("USER_NAME", (String)response.get("usr_name"));
				Log.d("MOBILE", (String)response.get("mob"));
				
				openMain();
			}
		}
		else if(responseString[0].equals("200") && Integer.parseInt(responseString[2]) == 2){
			HashMap<String, Object> response =(HashMap<String, Object>) JSONParser.convertJSON2Hashmap(responseString[1]);
			if(response.get("post") != null){
				if(response.get("post").equals("0")){
					Toast.makeText(this, "Password sent to your E-Mail", Toast.LENGTH_LONG).show();
				}else if(response.get("post").equals("1")){
					Toast.makeText(this, "E-Mail not found", Toast.LENGTH_LONG).show();
				}else if(response.get("post").equals("2")){
					Toast.makeText(this, "Error occured in sending email", Toast.LENGTH_LONG).show();
				}else if(response.get("post").equals("3")){
					Toast.makeText(this, "Server error, try again later", Toast.LENGTH_LONG).show();
				}
			}
		}
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if(requestCode==11 && resultCode==111)openMain();
		else super.onActivityResult(requestCode, resultCode, data);
	}
	
	private void openMain(){
		startActivity(new Intent(this, MainActivity.class));
		finish();
	}
}